import com.microsoft.azure.storage.CloudStorageAccount;
import com.microsoft.azure.storage.StorageException;
import com.microsoft.azure.storage.blob.CloudBlobClient;
import com.microsoft.azure.storage.blob.CloudBlobContainer;
import com.microsoft.azure.storage.blob.CloudBlockBlob;

import java.io.*;
import java.net.URISyntaxException;
import java.security.InvalidKeyException;

public class AzureClient {
    public static final String storageConnectionString = "";

    static {
        System.setProperty("https.proxyHost", "proxy.cse.cuhk.edu.hk");
        System.setProperty("https.proxyPort", "8000");
        System.setProperty("http.proxyHost", "proxy.cse.cuhk.edu.hk");
        System.setProperty("http.proxyPort", "8000");
    }

    private CloudBlobContainer container = null;

    public void initClient() {
        // Retrieve storage account from connection-string.
        CloudStorageAccount storageAccount = null;
        try {
            storageAccount = CloudStorageAccount.parse(storageConnectionString);
            // Create the blob client.
            CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
            // Retrieve reference to a previously created container.
            this.container = blobClient.getContainerReference("mycontainer");
            // Create the container if it does not exist.
            this.container.createIfNotExists();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (StorageException e) {
            e.printStackTrace();
        }
    }

    public void upload(String remoteFileName, String localFileName) {
        if (container == null) {
            initClient();
        }
        // Create or overwrite the remoteFileName blob with contents from a local file.
        try {
            CloudBlockBlob blob = container.getBlockBlobReference(remoteFileName);
            File source = new File(localFileName);
            blob.upload(new FileInputStream(source), source.length());
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (StorageException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void download(String remoteFileName, String localFileName) {
        if (container == null) {
            initClient();
        }
        // Create or overwrite the remoteFileName blob with contents from a local file.
        try {
            // Retrieve reference to a blob named "remoteFileName".
            CloudBlockBlob blob = container.getBlockBlobReference(remoteFileName);
            blob.download(new FileOutputStream(localFileName));
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (StorageException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void delete(String fileName) {
        if (container == null) {
            initClient();
        }
        // Retrieve reference to a blob named "fileName".
        try {
            CloudBlockBlob blob = container.getBlockBlobReference(fileName);
            // Delete the blob.
            blob.deleteIfExists();
        } catch (StorageException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

}
